package com.zjb.demo.system.controller;

import com.zjb.demo.common.constant.Constants;
import com.zjb.demo.common.constant.UserConstants;
import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.common.domain.response.TreeSelect;
import com.zjb.demo.system.domain.SysMenu;
import com.zjb.demo.system.domain.vo.RoleMenuVo;
import com.zjb.demo.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author 庄佳彬
 * @since 2023/12/29 21:44
 */
@Controller
@Api(tags = "菜单信息")
@Mapping("/system/menu")
public class MenuController {
    @Inject
    private SysMenuService menuService;

    @Get
    @Mapping("/list")
    @ApiOperation(value = "查询菜单列表")
    public AjaxResult<List<SysMenu>> list(SysMenu menu) {
        return AjaxResult.success(menuService.selectMenuList(menu));
    }

    @Get
    @Mapping("/{menuId}")
    @ApiOperation(value = "根据菜单编号获取详细信息")
    public AjaxResult<SysMenu> get(Long menuId) {
        return AjaxResult.success(menuService.getById(menuId));
    }

    @Get
    @Mapping("/treeselect")
    @ApiOperation("获取菜单下拉树列表")
    public AjaxResult<List<TreeSelect>> treeSelect(SysMenu menu) {
        return AjaxResult.success(menuService.buildMenuTreeSelect(menu));
    }

    @Get
    @Mapping(value = "/roleMenuTreeselect/{roleId}")
    @ApiOperation("获取角色菜单下拉树列表")
    public AjaxResult<RoleMenuVo> roleMenuTreeSelect(Long roleId) {
        var menus = menuService.buildRoleMenuTreeSelectByUserId();
        var checkedKeys = menuService.selectMenuListByRoleId(roleId);
        return AjaxResult.success(new RoleMenuVo(checkedKeys, menus));
    }

    @Post
    @Mapping
    @ApiOperation(value = "新增菜单")
    public AjaxResult<Boolean> add(@Body SysMenu menu) {
        if (menuService.exists(menu)) {
            return AjaxResult.fail("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
            return AjaxResult.fail("新增菜单'" + menu.getMenuName() + "'失败，地址必须为相对地址，不能是http(s)://开头");
        }
        return AjaxResult.success(menuService.save(menu));
    }
    @Put
    @Mapping
    @ApiOperation(value = "修改菜单")
    public AjaxResult<Boolean> edit(@Body SysMenu menu) {
        if (menuService.exists(menu)) {
            return AjaxResult.fail("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
            return AjaxResult.fail("新增菜单'" + menu.getMenuName() + "'失败，地址必须为相对地址，不能是http(s)://开头");
        }else if (menu.getMenuId().equals(menu.getParentId())) {
            return AjaxResult.fail("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        return AjaxResult.success(menuService.updateById(menu,true));
    }
    @Delete
    @Mapping("/{menuId}")
    @ApiOperation(value = "删除菜单")
    public AjaxResult<Boolean> delete(Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return AjaxResult.fail("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return AjaxResult.fail("菜单已分配,不允许删除");
        }
        return AjaxResult.success(menuService.removeById(menuId));
    }
}
